package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.entity.Drug;
import com.neuedu.mapper.DrugMapper;
import com.neuedu.service.DrugService;
import com.neuedu.vo.DrugCondition;
import com.neuedu.vo.DrugRegCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DrugServiceImpl extends ServiceImpl<DrugMapper, Drug> implements DrugService {

    @Autowired
    private DrugMapper drugMapper;

    @Override
    public Page<Drug> getDrugList(DrugCondition drugCondition) {

        System.out.println(drugCondition);

        Page page = new Page(drugCondition.getCurrentPage(), drugCondition.getPageSize());

        QueryWrapper<Drug> wrapper = new QueryWrapper<>();

        wrapper.like("drug_name", drugCondition.getDrugName());
        wrapper.like("drug_code", drugCondition.getDrugCode());

        if(drugCondition.getDrugType()!=null) {
            wrapper.eq("drug_type", drugCondition.getDrugType());
        }

        if(drugCondition.getMinPrice()!=null) {
            wrapper.ge("drug_price", drugCondition.getMinPrice());
        }

        if(drugCondition.getMaxPrice()!=null) {
            wrapper.le("drug_price", drugCondition.getMaxPrice());
        }

        return drugMapper.selectPage(page, wrapper);

    }

    @Override
    public boolean removeEnable(int drugId) {
        return drugMapper.findPrescriptionCount(drugId)==0;
    }

    @Override
    public boolean deleteBatch(String drugIds) {

        //将字符串拆分成数组  "1,2,3" --> [1,2,3]
        String[] arr = drugIds.split(",");

        //字符串数组转换集合  [1,2,3] --> {1,2,3}
        List<Integer> list = new ArrayList<>();

        for (String s : arr) {
            list.add(Integer.parseInt(s));
        }

        return this.removeByIds(list);
    }

    @Override
    public List<Drug> getDrugList(DrugRegCondition drugRegCondition) {
        return this.query()
                .like("drug_name", drugRegCondition.getDrugName())
                .like("drug_code", drugRegCondition.getDrugCode())
                .list();
    }


}
